package controllers;

import play.*;
import play.mvc.*;
import play.data.*;
import models.*;
import views.html.*;

/** 
 * This is my first project on play framework
 */
public class Application extends Controller {
	
	static Form<Task> taskForm = Form.form(Task.class);
	static HelloJava helloJava = new HelloJava();

    public static Result index() {
        //return ok(index.render("Your new application is ready."));
        return redirect(routes.Application.tasks());
    }
    
    public static Result tasks() {
    	return ok(views.html.index.render(Task.all(), taskForm, helloJava.hello()));
    }
      
    public static Result newTask() {
    	Form<Task> filledForm = taskForm.bindFromRequest();
    	if(filledForm.hasErrors()) {
    	  return badRequest(
    	    views.html.index.render(Task.all(), filledForm, helloJava.hello())
    	  );
    	} else {
    	  Task.create(filledForm.get());
    	  return redirect(routes.Application.tasks());  
    	}
    }
      
    public static Result deleteTask(Long id) {
    	Task.delete(id);
    	return redirect(routes.Application.tasks());
    }

}
